package model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class GameManager {

	private ArrayList<Game> games;
	private ArrayList<Player> players;
    private Map<Game, ArrayList<Player>> allPlayers;
	
	public GameManager() {
		games = new ArrayList<Game>();
		players = new ArrayList<Player>();
        allPlayers = new HashMap<Game, ArrayList<Player>>();
	}

    public Map<Game, ArrayList<Player>> getAllPlayers(){
        return allPlayers;
    }
	
    //Vraag de games op die nog niet vol zijn.
	public ArrayList<Game> getJoinableGames() {
		ArrayList<Game> joinableGames = new ArrayList<Game>();
		for(Game g : games) {
			if(g.getPlayers().size() < 4) {
				joinableGames.add(g);
			}
		}
		return joinableGames;
	}
	
	public ArrayList<Game> getGames() {
		return games;
	}

	//Zoek game met 'gameID' op.
    public synchronized Game getGame(String gameID){
        for(Game game: games){
            if(game.getName().equals(gameID))
                return game;
        }
        return null;
    }
	
    //Laat een bepaalde speler een bepaalde game joinen.
	public Game joinGame(String name, Player player) {
		for(Game g : games) {
			if(g.getName().equals(name)) {
				if(g.getPlayers().size() == 4) {
					//Spel vol.
					System.out.println("Spel zat vol");
				}
				else {
					g.newPlayer(player);
					return g;
				}
			}
		}
		//Game niet gevonden.
		System.out.println("Game kon niet gevonden worden");
		return null;
	}
	
	//Nieuwe game aanmaken.
	public Game newGame(String name, Player player, int blind) {
		boolean exists = false;
		for(Game g : games) {
			if(g.getName().equals(name)) {
				exists = true;
				//Er bestaat al een game met dezelfde naam.
			}
		}
		if(exists == false) {
			Game g = new Game(name, player, blind);
			games.add(g);
			return g;
		}
		return null;
	}
	
}